package com.sim.action;



import java.util.Date;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.sim.base.BaseAction;
import com.sim.domain.Society;
import com.sim.domain.SocietyType;
import com.sim.util.QueryHelper;
@Controller
@Scope("prototype")
@SuppressWarnings("serial")
public class SocietyMAction extends BaseAction<Society> {
	private Long societyTypeId;
	/**
	 * true 表示升序<br>
	 * false 表示降序
	 */
	private boolean asc = false;
	//主页面
    public String list() throws Exception {
    	new QueryHelper(Society.class, "s")//
    	    .addOrderProperty(true, "s.currentTime", asc) //  
    	    .preparePageBean(societyService, pageNum, pageSize);		    
       
    	return "list";
    }
    /** 删除 */
	public String delete() throws Exception {
		societyService.delete(model.getId());
		return "toList";
	}

	/** 添加页面 */
	public String addUI() throws Exception {
		// 准备回显的数据
		List<SocietyType> societyTypeList = societyTypeService.findAll();
		ActionContext.getContext().put("societyTypeList", societyTypeList);

		return "saveUI";
	}

	/** 添加 */
	public String add() throws Exception {
		SocietyType societyType = societyTypeService.getById(societyTypeId);
		model.setSocietyType(societyType);
		societyService.save(model);
		return "toList";
	}

	/** 修改页面 */
	public String editUI() throws Exception {
		List<SocietyType> societyTypeList = societyTypeService.findAll();
		ActionContext.getContext().put("societyTypeList", societyTypeList);
		
		// 准备回显的数据
		societyTypeId =model.getId();
		Society society = societyService.getById(model.getId());
		ActionContext.getContext().getValueStack().push(society);
		return "saveUI";
	}

	/** 修改 */
	public String edit() throws Exception {
		// 1，从数据库中取出原对象
		Society society = societyService.getById(model.getId());
		// 2，设置要修改的属性
		society.setTitle(model.getTitle());
        society.setContent(model.getContent());
        society.setCurrentTime(new Date());
        society.setLeader(model.getLeader());
		// 3，更新到数据库
		societyService.update(society);

		return "toList";
	}
	//===================
	public Long getSocietyTypeId() {
		return societyTypeId;
	}
	public void setSocietyTypeId(Long societyTypeId) {
		this.societyTypeId = societyTypeId;
	}
	

	
    
}
